package com.promis.widget.mediapicker.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.promis.util.CollectionUtils;
import com.promis.widget.R;
import com.promis.widget.listener.OnAvoidMultiClickListener;
import com.promis.widget.mediapicker.entity.ImageParcelable;
import com.promis.widget.mediapicker.manager.ImagePreviewManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jidanbo on 2018/3/26.
 */

public class ImagePreviewActivity extends AppCompatActivity {

    private ImageView imgvClose;
    private TextView txtvTitle;

    private Button btnvDelete;

    private ViewPager viewpager;
    private AdapterFragment adapterFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mediapicker_preview);

        imgvClose = findViewById(R.id.activity_media_picker_imgv_close);
        txtvTitle = findViewById(R.id.activity_media_picker_txtv_title);

        btnvDelete = findViewById(R.id.activity_media_picker_btnv_delete);

        viewpager = findViewById(R.id.activity_media_picker_viewpager);

        initData();
        initListener();
    }

    private void initData() {
        if (CollectionUtils.isNullOrEmpty(ImagePreviewManager.getInstance().getImageList())) return;

        ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
        for (ImageParcelable image : ImagePreviewManager.getInstance().getImageList()) {
            fragmentArrayList.add(ImagePreviewFragment.newInstance(image, false));
        }
        adapterFragment = new AdapterFragment(getSupportFragmentManager(), fragmentArrayList);
        viewpager.setAdapter(adapterFragment);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageSelected(int position) {
                txtvTitle.setText((position + 1) + " / " + ImagePreviewManager.getInstance().getImageList().size());
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
        viewpager.setCurrentItem(ImagePreviewManager.getInstance().getSelectPosition());
        txtvTitle.setText((ImagePreviewManager.getInstance().getSelectPosition() + 1) + " / " + ImagePreviewManager.getInstance().getImageList().size());
    }

    private void initListener() {
        imgvClose.setOnClickListener(new OnAvoidMultiClickListener() {
            @Override
            public void onAvoidMultiClick(View v) {
                finish();
            }
        });


        btnvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int currentIndex = viewpager.getCurrentItem();

                int futureIndex = currentIndex;
                if (currentIndex == adapterFragment.getCount() - 1) {
                    futureIndex = currentIndex - 1;
                }

                ImageParcelable image = ImagePreviewManager.getInstance().getImageList().get(currentIndex);
                ImagePreviewManager.getInstance().getImageList().remove(image);
                adapterFragment.removeItem(currentIndex);

                if (adapterFragment.getCount() == 0) finish();

                viewpager.setCurrentItem(futureIndex);
                txtvTitle.setText((futureIndex + 1) + " / " + adapterFragment.getCount());

//                if (ImagePreviewManager.getInstance().getImageList().size() == 0) {
//                    finish();
//                } else if (ImagePreviewManager.getInstance().getImageList().size() > 1) {
//                    viewpager.setCurrentItem(currentIndex + 1);
//                    adapterFragment.removeItem(currentIndex);
//                    txtvTitle.setText((currentIndex + 1) + "/" + ImagePreviewManager.getInstance().getImageList().size());
//                } else if (currentIndex > 1 && ) {
//                    adapterFragment.removeItem(currentIndex);
//                    viewpager.setCurrentItem((currentIndex + 1));
//                    txtvTitle.setText((currentIndex + 1) + "/" + ImagePreviewManager.getInstance().getImageList().size());
//                }
                if (ImagePreviewManager.getInstance().getMediaPreviewListener() != null) {
                    ImagePreviewManager.getInstance().getMediaPreviewListener().onDeleteCompleted(image);
                }
            }
        });
    }

    public class AdapterFragment extends FragmentStatePagerAdapter {
        private List<Fragment> mFragments;

        public AdapterFragment(FragmentManager fm, List<Fragment> mFragments) {
            super(fm);
            this.mFragments = mFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        public void removeItem(int position) {
            this.mFragments.remove(position);
            super.notifyDataSetChanged();
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return PagerAdapter.POSITION_NONE;
        }
    }
}
